package com.example.administrator.myapplication.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.palyActivity.PlayActivity;
import com.example.administrator.myapplication.databinding.ActivityMainBinding;
import com.example.administrator.myapplication.fragment.CenterListenerFragment;
import com.example.administrator.myapplication.fragment.DownLoadFragment;
import com.example.administrator.myapplication.fragment.FindFragment;
import com.example.administrator.myapplication.fragment.MySelfFragment;
import com.example.administrator.myapplication.fragment.SubscribeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private List<Fragment> fragments;
    private String nickName;
    private String img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        nickName = getIntent().getStringExtra("nickName");
        img = getIntent().getStringExtra("img");
        aboutFragmentManager();
        aboutGroupListener();
    }

    private void aboutGroupListener() {
        binding.mainGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //transaction = manager.beginTransaction();
                FragmentManager manager = getSupportFragmentManager();
                switch (checkedId) {
                    case R.id.main_radio_button1:
                        manager.beginTransaction().show(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).commit();
                        break;
                    case R.id.main_radio_button2:
                        manager.beginTransaction().show(fragments.get(1)).hide(fragments.get(0)).hide(fragments.get(2)).hide(fragments.get(3)).commit();
                        break;
                    case R.id.main_radio_button3:
                        Intent intent = new Intent(MainActivity.this,PlayActivity.class);
                        MainActivity.this.startActivity(intent);
                        binding.mainRadioButton3.setChecked(false);
                      //  manager.beginTransaction().show(fragments.get(2)).hide(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(3)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.main_radio_button4:
                        manager.beginTransaction().show(fragments.get(3)).hide(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(4)).commit();
                        break;
                    case R.id.main_radio_button5:
                        manager.beginTransaction().show(fragments.get(4)).hide(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).commit();
                        break;
                }
            }
        });

    }

    private void aboutFragmentManager() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        fragments = new ArrayList<>();
        FindFragment findFragment = new FindFragment();
        SubscribeFragment subscribeFragment = new SubscribeFragment();
        CenterListenerFragment centerListenerFragment = new CenterListenerFragment();
        DownLoadFragment downLoadFragment = new DownLoadFragment();
        MySelfFragment mySelfFragment = MySelfFragment.newInstance(nickName,img);
        fragments.add(findFragment);
        fragments.add(subscribeFragment);
        fragments.add(centerListenerFragment);
        fragments.add(downLoadFragment);
        fragments.add(mySelfFragment);
        transaction.add(R.id.main_container, fragments.get(0)).show(fragments.get(0))
                .add(R.id.main_container, fragments.get(1)).hide(fragments.get(1))
                .add(R.id.main_container, fragments.get(2)).hide(fragments.get(2))
                .add(R.id.main_container, fragments.get(3)).hide(fragments.get(3))
                .add(R.id.main_container, fragments.get(4)).hide(fragments.get(4))
                .commit();
    }
}
